/*
*/
ALTER PROC SP_SAVE_FORECAST_DATA ( 
					@PROJID INT,
					@PRODID INT,
					@YEAR INT,
					@QTR INT,
					@QTY INT,
					@PRICE FLOAT,
					@USER_ID VARCHAR(50),
					@TRANSACTION_ID VARCHAR(100))
AS
BEGIN
	DECLARE @ROW_ID INT
	SET @ROW_ID = NULL
	SELECT @ROW_ID = ROWID FROM FCENTRYMASTER WHERE PROJID = @PROJID AND PRODID = @PRODID AND [QUARTER] = @QTR AND [YEAR] = @YEAR

	IF( @ROW_ID IS NULL ) --FORECAST NOT EXIST FOR THIS PROJ, PROD, QUARTER, YEAR
	 BEGIN
		INSERT INTO FCENTRYMASTER ( SPERSONID, PRODID, CUSTID, ODMID, PROJID, [QUARTER], YEAR, QTY, PRICE, USERID, ENTRYDATE, USERTXT2 )
			SELECT SPERSONID, @PRODID, CUSTID, ODMID, @PROJID, @QTR, @YEAR, @QTY, @PRICE, @USER_ID, CURRENT_TIMESTAMP, @TRANSACTION_ID FROM PROJSETUP WHERE PROJID = @PROJID	
	 END
	ELSE
	 BEGIN
/*
		--copy to history table
		INSERT INTO FCENTRYHISTORY ( ROWID, SPERSONID, PRODID, CUSTID, ODMID,PROJID, 
					MONTH, QUARTER, YEAR, QTY, PRICE,CONFIDENCE_LEVEL, USERLIST1, 
					USERDATE1, USERTXT1,USERTXT2, USERID, ENTRYDATE, COMMENTS )
			( SELECT ROWID, SPERSONID, PRODID, CUSTID, ODMID,PROJID,MONTH, QUARTER, YEAR, 
				QTY, PRICE,CONFIDENCE_LEVEL, USERLIST1, USERDATE1, USERTXT1,USERTXT2, 
				USERID, ENTRYDATE, COMMENTS FROM FCENTRYMASTER WHERE ROWID = @ROW_ID )
*/		
		--now update
		DECLARE @SPERSONID INT
		DECLARE @CUSTID INT
		DECLARE @ODMID INT
		SELECT @SPERSONID = SPERSONID, @CUSTID = CUSTID, @ODMID = ODMID FROM PROJSETUP WHERE PROJID = @PROJID	
		UPDATE FCENTRYMASTER 
			SET QTY = @QTY, 
				PRICE = @PRICE, 
				CONFIDENCE_LEVEL = 0.0, 
				USERID = @USER_ID, 
				SPERSONID = @SPERSONID,
				CUSTID = @CUSTID, 
				ODMID = @ODMID,
				ENTRYDATE = CURRENT_TIMESTAMP,
				USERTXT2 = @TRANSACTION_ID
			WHERE ROWID = @ROW_ID
	 END	
END